<?php
$this->breadcrumbs=array(
	'报销单外部凭证编号',
);

$this->userActions=array(
	//array('label'=>'List MaterialReceipt', 'url'=>array('index')),
	//array('label'=>'管理单据', 'url'=>array('admin')),
);
?>

<h1>报销单外部凭证编号</h1>

<div class="form">
<?php $form = $this
	->beginWidget('CActiveForm',
		array(
			'id' => 'material-expense-attach-external-form',
			'enableAjaxValidation' => false,
		));
?>
	<p class="note">带 <span class="required">*</span> 的是必填的哦！</p>

<?php if(Yii::app()->user->hasFlash('attachExpenseExternal')){?>
    <div class="flash-success">
        <?php echo Yii::app()->user->getFlash('attachExpenseExternal'); ?>
    </div>
<?php } ?>

	<?php echo $form->errorSummary($model); ?>

	<div class="row">
		<?php echo $form->labelEx($model, 'externalTokenDate'); ?>
		<?php $this
			->widget('zii.widgets.jui.CJuiDatePicker',
				array(
					'model' => $model,
					'attribute' => 'externalTokenDate',
				));
		?>
		<?php echo $form->error($model, 'externalTokenDate'); ?>
	</div>
	<div class="row">
		<?php echo $form->labelEx($model, 'externalTokenNumber'); ?>
		<?php echo $form
			->textField($model, 'externalTokenNumber', array(
				'size' => 30,
				'maxlength' => 255
			));
		?>
		<?php echo $form->error($model, 'externalToken'); ?>
	</div>
		<div class="row">
		<?php echo $form->labelEx($model, 'materialExpenseIds'); ?>
		<?php echo CHtml::textField('materialExpenseBarcode'); ?>
		<div id="materialExpenseIdsContainer">
		</div>
		<?php echo $form->error($model, 'materialExpenseIds'); ?>
	</div>
<?php 
$dynamicLoadExpenseUrl = CJSON::encode($this->createUrl('materialReceipt/dynamicFindExpense'));
$viewAccountTokenUrl = CJSON::encode($this->createUrl('accountToken/view',array('id'=>'_accountTokenId')));
$labelHtml=CJSON::encode(CHtml::label('&nbsp;',false));
$expenseIdCheckboxName=CHtml::activeName($model, 'materialExpenseIds[]');
$js = <<<EOF
$('#materialExpenseBarcode').keypress(function(event) {
	if ( event.which == 13 ) {
		event.preventDefault();
		var expenseBarcode=this.value;
		jQuery.get($dynamicLoadExpenseUrl, {barcode:expenseBarcode},function(data, textStatus, jqXHR){
		var checkbox, describeContainer, singleContainer;
		if(data.accountTokenId){
				var confirmed=confirm("此报销单已附于内部凭证，无法再次附于外部凭证\\n是否退出本页面并查看此凭证？");
				if(confirmed){
					window.location.href=($viewAccountTokenUrl.replace('_accountTokenId',data.accountTokenId));
				}
				return;
			}
		singleContainer=$('<div></div>');
		checkbox=$('<input type="checkbox" />');
		if(!data.externalTokenNumber){
			checkbox.attr('checked','checked');
		}
		checkbox.attr('name','$expenseIdCheckboxName');
		checkbox.attr('value',data.id);
		describeContainer=$('<span></span>');
		describeContainer.html(' '+ data.expenseDate +' '+ data.project.name +' '+ data.totalPrice +(data.externalTokenNumber?('（已附于凭证：'+data.externalTokenDate+'#'+data.externalTokenNumber+'）'):''));
		singleContainer.append($labelHtml).append(checkbox).append(describeContainer);
		$('#materialExpenseIdsContainer').append(singleContainer);
        }, 'json' );
		this.value='';
	}
});
EOF
;
Yii::app()->clientScript->registerScript('dynamic-material-expense-search', $js);

?>
	<div class="row buttons">
		<?php echo CHtml::submitButton('保存'); ?>
	</div>
<?php $this->endWidget(); ?>

</div><!-- form -->